package com.cos.openapi.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

/**
 * @author: COS
 * @time: 2021/1/29 11:12
 * @description:
 */
public class Person {
    //id
    private Integer id;
    private String name;
    private Integer age;
    //父级id
    private Integer pre_id;
    //保存子级数据
    private List<Person> children = new ArrayList<>();

    public Person(Integer id, String name, Integer age, Integer pre_id) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.pre_id = pre_id;
    }
/*
    public static void main(String[] args) {
        Person p = new Person();
        p.renzu();
    }*/


    public void fx2(Person a) {
        a.setName("娃哈哈");
        System.out.println("fx中修改后的值:" + a);
    }

    public void fx1(int a) {
        a += 10;
        System.out.println("fx中修改后的值:" + a);
    }

    public static void main(String[] args) {
        Person p = new Person();
        int a = 10;
        System.out.println("修改前的值:" + a);
        p.fx1(a);
        System.out.println("修改后的值:" + a);

        /*Person p = new Person();
        p.setName("张三");
        System.out.println("修改前的值:"+p);
        p.fx2(p);
        System.out.println("修改后的值:"+`p`);*/


    }

    public void renzu() {
        Person p1 = new Person(1, "张三", 50, null);
        Person p2 = new Person(2, "李四", 50, null);
        Person p3 = new Person(3, "王五", 50, null);
        Person p21 = new Person(4, "李四一", 50, 2);
        Person p22 = new Person(5, "李四二", 50, 2);
        Person p31 = new Person(6, "王五一", 50, 3);
        Person p23 = new Person(7, "狗蛋", 50, 2);
        //所有人的数据
        List<Person> peoples = Arrays.asList(p1, p2, p3, p21, p22, p31, p23);
        System.out.println("==============所有人的信息=================");
        for (Person people : peoples) {
            System.out.println(people);
        }

        System.out.println("==============找到孩子了=================");
        for (Person person : getInfo(peoples)) {
            System.out.println(person);
        }
    }

    public List<Person> getInfo(List<Person> peoples) {
        //保存所有父亲
        List<Person> peopleInfo = new ArrayList<>();
        Map<Integer, Person> map = new HashMap<>();
        //在数据当中找到父亲
        for (Person people : peoples) {
            //保存信息到寻人网站
            map.put(people.getId(), people);
            //没有父级id 即没有父亲 是1级菜单(孤儿)
            if (people.getPre_id() == null) {
                peopleInfo.add(people);
            }
        }

        for (Person people : peoples) {
            //知道有父亲,并且在寻人网站有父亲的信息存在
            if (people.getPre_id() != null && map.containsKey(people.getPre_id())) {
                //找到父亲
                Person person = map.get(people.getPre_id());
                //和这个父亲说我的手上有你的孩子,你懂得
                person.getChildren().add(people);
            }
        }
        return peopleInfo;
    }

    public Integer getId() {
        return id;
    }

    public Person() {
    }

    public Person(Integer id, String name, Integer age, Integer pre_id, List<Person> children) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.pre_id = pre_id;
        this.children = children;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getPre_id() {
        return pre_id;
    }

    public void setPre_id(Integer pre_id) {
        this.pre_id = pre_id;
    }

    public List<Person> getChildren() {
        return children;
    }

    public void setChildren(List<Person> children) {
        this.children = children;
    }
}
